Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Telephony

Namespace C4F.DevKit.Telephony.Controls
	Public Partial Class PlaybackAndRecordingPanel
		Inherits UserControl
		#Region "Members"

		Private _tapiManager As TapiManager

		Private _openFileDialog As OpenFileDialog

		Private _folderBrowserDialog As FolderBrowserDialog

		#End Region

		#Region "Properties"

		Public Property TapiManager() As TapiManager
			Get
				Return _tapiManager
			End Get

			Set(ByVal value As TapiManager)
				_tapiManager = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property PlayStopButtonsEnabled() As Boolean
			Get
				Return Me.btnPlay.Enabled
			End Get

			Set(ByVal value As Boolean)
				Me.btnPlay.Enabled = value
				Me.btnStop.Enabled = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property CallRecordingControlsEnabled() As Boolean
			Get
				Return txtFileName.Enabled
			End Get

			Set(ByVal value As Boolean)
				txtFileName.Enabled = value
				btnDirectory.Enabled = value
				btnRecordingStart.Enabled = value
				btnRecordingStop.Enabled = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property PlayBackControlsEnabled() As Boolean
			Get
				Return btnStop.Enabled
			End Get

			Set(ByVal value As Boolean)
				lstBoxFileNames.Enabled = value
				btnBrowse.Enabled = value
				btnPlay.Enabled = value
				btnStop.Enabled = value
			End Set
		End Property

		<Browsable(True)> _
		Public Property PlaybackEnabled() As Boolean
			Get
				Return grpBoxPlayRecord.Enabled
			End Get

			Set(ByVal value As Boolean)
				grpBoxPlayRecord.Enabled = value
			End Set
		End Property

		#End Region

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			AddHandler btnRecordingStop.Click, AddressOf btnRecordingStop_Click
			AddHandler btnStop.Click, AddressOf btnStop_Click
			AddHandler lstBoxFileNames.SelectedIndexChanged, AddressOf lstBoxFileNames_SelectedIndexChanged
			AddHandler btnPlay.Click, AddressOf btnPlay_Click
			AddHandler btnBrowse.Click, AddressOf btnBrowse_Click
			AddHandler btnRecordingStart.Click, AddressOf btnRecordingStart_Click
			AddHandler btnDirectory.Click, AddressOf btnDirectory_Click
			AddHandler rdCallRecording.CheckedChanged, AddressOf radioButton2_CheckedChanged
			AddHandler rdPlayBack.CheckedChanged, AddressOf radioButton1_CheckedChanged

			_folderBrowserDialog = New FolderBrowserDialog()
			_openFileDialog = New OpenFileDialog()
		End Sub

		#End Region

		#Region "Event Handlers"

		Private Sub btnRecordingStop_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				_tapiManager.StopCallRecording()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnStop_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				_tapiManager.StopPlayingFile()
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub lstBoxFileNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
			PlayStopButtonsEnabled = lstBoxFileNames.Items.Count > 0
		End Sub

		Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				If Not lstBoxFileNames.SelectedItem Is Nothing Then
					_tapiManager.PlayFile(lstBoxFileNames.SelectedItem.ToString())
				Else
					MessageBox.Show("Please select file to play.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
				End If
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnBrowse_Click(ByVal sender As Object, ByVal e As EventArgs)
			_openFileDialog.Filter = "wav files (*.wav)|*.wav"
			_openFileDialog.RestoreDirectory = True
			_openFileDialog.Multiselect = True
			If _openFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				lstBoxFileNames.Items.Clear()
				lstBoxFileNames.Items.AddRange(_openFileDialog.FileNames)
			End If
		End Sub

		Private Sub btnRecordingStart_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				If txtFileName.Text = String.Empty Then
					MessageBox.Show("File name not specified.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Else
					Dim targetFilePath As String = String.Empty
					targetFilePath &= _folderBrowserDialog.SelectedPath & "\" & txtFileName.Text
					targetFilePath = targetFilePath.Replace("\\", "\")
					targetFilePath = targetFilePath.Trim("\"c)
					_tapiManager.RecordCall(targetFilePath)
				End If
			Catch exception As Exception
				MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End Sub

		Private Sub btnDirectory_Click(ByVal sender As Object, ByVal e As EventArgs)
			_folderBrowserDialog.ShowDialog()
		End Sub

		Private Sub radioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
			If rdCallRecording.Checked = True Then
				CallRecordingControlsEnabled = True
				PlayBackControlsEnabled = False
			End If
		End Sub

		Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
			If rdPlayBack.Checked = True Then
				PlayBackControlsEnabled = True
				CallRecordingControlsEnabled = False
			End If
		End Sub

		#End Region
	End Class
End Namespace
